package org.gangcai.mac.shop.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.blankj.utilcode.util.SPUtils;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.bean.CommonResonseBean;
import org.gangcai.mac.shop.bean.ExtentionBean;
import org.gangcai.mac.shop.common.MultiStatusActivity;
import org.gangcai.mac.shop.constants.Constant;
import org.gangcai.mac.shop.manager.RetrofitManager;
import org.gangcai.mac.shop.oberver.CommonObserver;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;

public class UserListActivity extends MultiStatusActivity {

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    private ArrayList dataList;
    private CommonAdapter<ExtentionBean.Info> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initTitleBarView(titlebar, "推广列表");
        initView();
        loadData();
    }

    private void initView() {
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        dataList = new ArrayList();
        adapter = new CommonAdapter<ExtentionBean.Info>(this, R.layout.user_list_adapter_item, dataList) {
            @Override
            protected void convert(ViewHolder holder, ExtentionBean.Info info, int position) {
                holder.setText(R.id.phone, info.getMobile());
                holder.setText(R.id.money, info.getInvite_money());
            }

        };
        recyclerview.setAdapter(adapter);
    }

    private void loadData() {
        multipleStatusView.showLoading();
        RetrofitManager.create().my_extension(SPUtils.getInstance().getString("uid"))
                .compose(this.<ExtentionBean>bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<ExtentionBean>(multipleStatusView) {
                    @Override
                    public void onSuccess(ExtentionBean value) {
                        if (value.getCode() == Constant.CODE_SUCC) {
                            multipleStatusView.showContent();
                            if (value.getInfo() == null) {
                                multipleStatusView.showEmpty();
                            } else {
                                dataList.addAll(value.getInfo());
                                adapter.notifyDataSetChanged();
                            }
                        }
                    }
                });
    }

    @Override
    public int getContentView() {
        return R.layout.activity_user_list;
    }


}
